<template>
  <a-button class="editable-add-btn" @click="handleAdd" style="margin-bottom: 8px">新增</a-button>
  <a-table :columns="columns" :data-source="dataSource" bordered>
    <template v-for="col in ['name', 'sex', 'zhiwu','zhicheng','begindate','enddate']" #[col]="{ text, record }" :key="col">
      <div>
        <a-input
          v-if="editableData[record.key]"
          v-model:value="editableData[record.key][col]"
          style="margin: -5px 0"
        />
        <template v-else>
          {{ text }}
        </template>
      </div>
    </template>
    <template #operation="{ record }">
      <div class="editable-row-operations">
        <span v-if="editableData[record.key]">
          <a @click="save(record.key)">保存</a>
          <a-popconfirm title="是否确认取消?" @confirm="cancel(record.key)">
            <a>取消</a>
          </a-popconfirm>
        </span>
        <span v-else>
          <a @click="edit(record.key)">变更</a>
        </span>
      </div>
      <a-popconfirm
        v-if="dataSource.length"
        title="是否确认删除?"
        @confirm="onDelete(record.key)"
      >
        <a>删除</a>
      </a-popconfirm>
    </template>
  </a-table>
</template>
<script>
import { cloneDeep } from 'lodash-es';
import { computed, defineComponent, reactive, ref } from 'vue';
const columns = [
  {
    title: '序号',
    dataIndex: 'index',
    width: '5%',
    slots: {
      customRender: 'name',
    },
  },
  {
    title: '姓名',
    dataIndex: 'name',
    width: '10%',
    slots: {
      customRender: 'name',
    },
  },
  {
    title: '性别',
    dataIndex: 'sex',
    width: '10%',
    slots: {
      customRender: 'sex',
    },
  },
  {
    title: '职务',
    dataIndex: 'zhiwu',
    width: '15%',
    slots: {
      customRender: 'zhiwu',
    },
  },
  {
    title: '职称',
    dataIndex: 'zhicheng',
    width: '15%',
    slots: {
      customRender: 'zhicheng',
    },
  },
  {
    title: '到岗日期',
    dataIndex: 'begindate',
    width: '15%',
    slots: {
      customRender: 'begindate',
    },
  },
  {
    title: '离岗日期',
    dataIndex: 'enddate',
    width: '15%',
    slots: {
      customRender: 'enddate',
    },
  },
  {
    title: 'operation',
    dataIndex: 'operation',
    width: '15%',
    slots: {
      customRender: 'operation',
    },
  },
];
const data = [];
for (let i = 0; i < 80; i++) {
  data.push({
    index:i+1,
    key: i.toString(),
    name: '黄国庆',
    sex: '男',
    zhiwu: '项目经理',
    zhicheng: '工程师',
    begindate: '2021-5-8',
    enddate: '2023-6-10',
  });
}
export default defineComponent({
  setup() {
    const count = computed(() => dataSource.value.length + 1);

    const dataSource = ref(data);
    const editableData = reactive({});
    const edit = key => {
      editableData[key] = cloneDeep(dataSource.value.filter(item => key === item.key)[0]);
    };
    const save = key => {
      Object.assign(dataSource.value.filter(item => key === item.key)[0], editableData[key]);
      delete editableData[key];
    };
    const cancel = key => {
      delete editableData[key];
    };

    const onDelete = key => {
      dataSource.value = dataSource.value.filter(item => item.key !== key);
    };
    const handleAdd = () => {
      const newData = {
        index:count.value,
        key: count.value.toString(),
        name: '',
        sex: '',
        zhiwu: '',
        zhicheng: '',
        begindate: '',
        enddate: '',
      };
      dataSource.value.push(newData);
      edit(count.value.toString());
    };
    return {
      dataSource,
      columns,
      editingKey: '',
      editableData,
      edit,
      save,
      cancel,
      onDelete,
      handleAdd
    };
  },
});
</script>
<style scoped>
.editable-row-operations a {
  margin-right: 8px;
}
</style>
